package com.example.day04;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class Recy_Adapter extends RecyclerView.Adapter<Recy_Adapter.ViewHolder> {
    List<Boolean> bool;
    boolean br;
    private Context context;
    private List<Bean.RecentBean> beanList;
    int ip;


    public Recy_Adapter(Context context, List<Bean.RecentBean> beanList) {
        this.context = context;
        this.beanList = beanList;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.activity_item, parent, false);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, final int position) {
        holder.t1.setText(beanList.get(position).getTitle());
        holder.t2.setText(beanList.get(position).getNews_id() + "");
        holder.t1.setText(beanList.get(position).getUrl() + "");
        holder.cx.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    Toast.makeText(context, "sss", Toast.LENGTH_SHORT).show();

                }
            }
        });
        if (br == true) {
            holder.cx.setVisibility(View.VISIBLE);
        } else {
            holder.cx.setVisibility(View.INVISIBLE);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onItemClic.getbean(position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return beanList.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        TextView t1;
        TextView t2;
        TextView t3;
        CheckBox cx;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            t1 = itemView.findViewById(R.id.text1);
            t2 = itemView.findViewById(R.id.text2);
            t3 = itemView.findViewById(R.id.text3);
            cx = itemView.findViewById(R.id.cb);
        }
    }

    OnItemClic onItemClic;

    public void setOnItemClic(OnItemClic onItemClic) {
        this.onItemClic = onItemClic;
    }

    public interface OnItemClic{
        void getbean(int i);
    }
    public void setData(List<Bean.RecentBean>beans,List<Boolean>bl){
        bool=bl;
        this.beanList=beans;
    }

}
